package cn.topcodes.tcsf.admin.web.websocket;

import cn.topcode.unicorn.utils.JsonUtil;
import cn.topcode.unicorn.utils.SpringContextHolder;
import org.apache.shiro.util.Assert;

/**
 * Created by Unicorn on 2018/2/7.
 */
public class AdminWebsocketHelper {

    public static void broadcastText(String text) {
        Assert.hasText(text, "推送文本不能为空");
        AdminSessionEndPoint endPoint = (AdminSessionEndPoint) SpringContextHolder.getBean("adminWebsocket");
        endPoint.broadcast(text);
    }

    public static void pushText(Long userId, String text) {
        Assert.notNull(userId, "推送userId不能为空");
        Assert.hasText(text, "推送文本不能为空");
        AdminSessionEndPoint endPoint = (AdminSessionEndPoint) SpringContextHolder.getBean("adminWebsocket");
        endPoint.sendText(userId, text);
    }

    public static void pushJson(Long userId, Object obj) {
        Assert.notNull(obj, "推送消息不能为空");
        pushText(userId, JsonUtil.toJson(obj));
    }

    public static void pushCmd(Long userId, String type, Object obj) {
        AdminWebSocketProtocol protocol = new AdminWebSocketProtocol();
        protocol.setType(type);
        protocol.setBody(obj);
        pushJson(userId, protocol);
    }
}
